/*


	jQuery plugin: Simple Accordion
	Author: Ian at Grand Union [ianh@thegrandunion.com]


	What?
	=====
	Super simple accordian functionality.


	How?
	====
	HTML:
	
		<dl class="accordion">
			<dt>Is this a question?</dt>
			<dd>
				<p>Yes it was. This is a the answer to the question.</p>
			</dd>
			<dt>Is this the second question?</dt>
			<dd>
				<p>It is! This is the answer to th second question.</p>
			</dd>
		</dl>
		
	JS:
	
		$('.accordion').simpleAccordian();
		
	A different HTML structure can be used - just make sure the 'trigger' and 'target' options are set accordingly.
	E.g.
	
	HTML:
	
		<ul class="accordion">
			<li>
				<strong>More information</strong>
				<p>Some more information here.</p>
			</li>
			<li>
				<strong>Even more information</strong>
				<p>Even more information here.</p>
			</li>
			<li>
				<strong>And yet more information</strong>
				<p>More information here.</p>
			</li>
		</ul>
		
	JS:
	
		$('ul.accordion').simpleAccordion({
			'trigger' : 'strong',
			'target' : 'p'
		});
	


	Options
	=======
	swap: A flag to determine whether revealing one target hides another.
	show: The function to execute when displaying a target.
	hide: The function to execute when hiding a target.
	trigger: Selector string for triggers.
	target:	Selector string for targets.
	
	
	Dependencies
	============
	- jQuery 1.4 or higher (jquery.com)


*/
(function($){

	$.fn.simpleAccordion = function(settings){

		var G = {
			config : {
				swap 		: false,
				show 		: function($el){ $el.show(); },
				hide 		: function($el){ $el.hide(); },
				trigger 	: 'dt',
				target 		: 'dd',
				openClass 	: 'open',
				closedClass : 'closed'
			},
			$this : this
		};

		if(settings){ $.extend(G.config, settings); }


		function init()
		{
			$(G.config.target, G.$this).hide();
			
			$(G.config.trigger, G.$this).hover(
				function(){ $(this).css('cursor', 'pointer'); },
				function(){ $(this).css('cursor', 'pointer'); }
			);

			if(G.config.swap===true)
			{
				$(G.config.trigger, G.$this).click(
					function()
					{
						// If there's a current item, hide it.
						var cur = $(this).parent().data('current');
						if(cur){
							G.config.hide($(cur).next());
							addCloseClass($(cur));
							$(this).parent().data('current', '');
						}

						// Show the new item and store it as the current item.
						if(cur!==this){
							G.config.show($(this).next());
							addOpenClass($(this));
							$(this).parent().data('current', this);
						}
					}
				);
			}
			else
			{
				$(G.config.trigger, G.$this).toggle(
					function(){
						G.config.show($(this).next());
						addOpenClass($(this));
					},
					function(){
						G.config.hide($(this).next());
						addCloseClass($(this));
					}
				);
			}
		}
		
		
		function addOpenClass(el)
		{
			$(el)
			.addClass(G.config.openClass)
			.removeClass(G.config.closedClass)
			.next()
			.addClass(G.config.openClass)
			.removeClass(G.config.closedClass);
		}
		
		
		function addCloseClass(el)
		{
			$(el)
			.addClass(G.config.closedClass)
			.removeClass(G.config.openClass)
			.next()
			.addClass(G.config.closedClass)
			.removeClass(G.config.openClass);
		}
		

		init();
		return this;

	};

})(jQuery);